//*****************************************************************************
//
//								Utilities
//
//*****************************************************************************
//
//
#include "Config.h"
#include "reg.h"
#include "typedefs.h"
#include "TW8835.h"
#include <stdarg.h>
#include <intrins.h>
#include "Util.h"
#include "printf.h"

//=============================================================================
//		Convert ASCII to Binery                                             
//=============================================================================
BYTE Asc1Bin(BYTE asc)
{
	if(asc>='0' && asc <='9') return (asc - '0');
	if(asc>='a' && asc <='f') return (asc - 'a' + 0x0a);
	if(asc>='A' && asc <='F') return (asc - 'A' + 0x0a);
	return 0;
}

DWORD a2h(BYTE *str)
{
	DWORD num=0;
	BYTE i;

	for(i=0; i<8; i++, str++) {
		if( *str=='\0' || *str==' ' ) break;
		num = num*0x10 + Asc1Bin(*str);
	}
	return num;
}

DWORD a2i(BYTE *str)
{
	DWORD num=0;
	BYTE i;

	for(i=0; ; i++, str++) {
		if( *str=='\0' || *str==' ' ) break;
		num = num*10 + *str - '0';
	}
	return num;
}

//=============================================================================
BYTE toupper(BYTE ch)
{
	if( ch>='a' && ch<='z' )
		return (ch - 'a' + 'A');

	return ch;
}

int stricmp(BYTE *ptr1, BYTE *ptr2)
{
	int	i, ret;
	
	for(i=0; *ptr1; i++) {
		ret = toupper(*ptr1++) - toupper(*ptr2++);
		if( ret ) return ret;
	}
	ret = toupper(*ptr1) - toupper(*ptr2);
	if( ret ) return ret;

	return 0;
}


//description
//	itoa
//	max 10000
//parameter
//	n 		input number
//	buff	output buffer. I do not check buff size. buff[] have to be big enough.	
//return
//	0 something wrong
//	n number of ascii character
#if 0
BYTE TWitoa(short n, BYTE *buff)
{
	BYTE next,start;
	WORD k, q;

	next = 0;
	start = 0;

	if(n < 0) {
		buff[next++] = '-';
		n = 0 - n;
		start++;
	}
	if(n == 0)
		buff[next++] = '0';
	else 
	{
		k = 10000;		 //max
		while( k > 0) 
		{
			if(n==0) 	q = 0;
			else		q = n / k;
			if((next!=start) || (q > 0)) { 
				buff[next++] = '0' + q;
			}
			n -= (q*k);
			k = k / 10;
		} 
	}
	buff[next] = 0x00;	//make string.		  
	return next;
}

BYTE TWstrlen(BYTE *s)
{
	BYTE *p = s;
	while(*p) p++;
	return p - s;
}
BYTE *TWstrcpy(BYTE *d,BYTE *s)
{
	BYTE *dd = d;
	while(*s)
		*dd++ = *s++;
	*dd = 0;

	return d;
}
BYTE *TWstrcat(BYTE *d,BYTE *s)
{
	BYTE *dd = d;
	while(*dd)
		dd++;
	while(*s)
		*dd++ = *s++;
	*dd = 0;

	return d;
}
BYTE IsDigit(BYTE c)
{
	if(c >= 0x30 && c <= 0x39)
		return 1;
	return 0;
}
#endif
//parameter
//	number of bytes
//	read bytes
//	register
//	write cmd data
//return
#if 0
BYTE SPI_cmd_protocol(BYTE max, ...)
{
	va_list ap;
	BYTE page;
	BYTE temp;
	BYTE r_cnt, w_cnt, i;
	BYTE w_cmd[5];
	BYTE ret;

	//-------------------
	ret=0xff;
	if(max < 2)
		return 0xff;
	
	va_start(ap, max);

	w_cnt = max-1;
	r_cnt = va_arg(ap, BYTE);		//r_cnt
	for(i=0; i < w_cnt; i++) {
		w_cmd[i]=va_arg(ap, BYTE);		//reg
	}
	Printf("\nSPICMD[%bd] r:%bd w:%bd reg:%bx",max, r_cnt, w_cnt-1, w_cmd[0]);

	page = ReadTW88(0xff);		//save
	WriteTW88Page( PAGE4_SPI );

	WriteTW88(0xc3, 0x40+w_cnt);
	Write2TW88(0xC6,0xC7, 0x04d0);			// DMA Page & Index. indecate DMA read/write Buffer at 0x04D0.
	WriteTW88( 0xDA,0 );					// DMA Length high
	Write2TW88(0xC8,0xC9, r_cnt);			// DMA Length middle & low

	//write 
	for(i=0; i < w_cnt; i++)
		WriteTW88( 0xca+i, w_cmd[i] );		// write cmd1
	if(r_cnt) {
		WriteTW88( 0xc4, 0x01 );				// start
		//delay1ms(2);
		for(i=0; i < 100; i++)
			_nop_();

	}
	else {	
		WriteTW88( 0xc4, 0x07 );				// start, with write, with busycheck
	}

	//read
	if(r_cnt)	Puts("\tREAD:");	
	for(i=0; i < r_cnt; i++) {
		temp = ReadTW88( 0xd0+i );
		Printf("%02bx ",temp);
		if(i==0)
			ret = temp;
	}							

	WriteTW88Page( page );	//restore
	//-------------------
	va_end(ap);

	return ret;
}
#endif
#if 0
void CheckRegister(BYTE page, BYTE reg, WORD line)
{
	BYTE saved_page;
	saved_page = ReadTW88(0xff);
	WriteTW88Page( page );
	Printf("\nCHECK:%d 0x%01bx%02bx:%bx",line, page, reg, ReadTW88(reg));
	WriteTW88Page(saved_page);
}
#endif
